import ruiDatePicker from '@/components/rattenking-dtpicker/rattenking-dtpicker.vue';
export default {
    components: {
        ruiDatePicker
    },
    data() {
        return {
            startDate: "", // 筛选开始时间
            endDate: "", // 筛选结束时间
            recordList: [], // 记录列表
            page: 1, // 当前页码
            noMore: false, // 没有更多数据了
        }
    },
    onLoad: function() {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var prevMonth = month <= 1 ? 12 : ((month - 1) < 10 ? "0" + (month - 1) : (month - 1));
        var startDate = (prevMonth == 12 ? (year - 1) : year) + "-" + prevMonth + "-" + "01";
        var endDate = year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);
        this.startDate = startDate;
        this.endDate = endDate;
        this.getAddStockRecord(); // 获取上货记录
    },
    methods: {

        // 获取上货记录
        getAddStockRecord: function() {
            let startTime = "";
            this.startDate.split("-").forEach((item, index) => {
                startTime += item;
            })
            let endTime = "";
            this.endDate.split("-").forEach((item, index) => {
                endTime += item;
            })
            let data = {
                userId: this.$store.state.userInfo.userId.toString(),
                startTime: startTime + '000000',
                endTime: endTime + '235900',
            }
            this.$request(this.$api.GetAddStockRecord, data, "POST").then(res => {
                if (res.returnCode == "SUCCESS") {
                    res.data = res.data || [];
                    if (this.page == 1) this.recordList = [];
                    res.data.forEach((item, index) => {
                        let optTime = item.optTime.split(" ");
                        item.createDate = optTime[0];
                        item.createTime = optTime[1];
                        item.detail = JSON.parse(item.detail || '[]')
                        item.addCount = 0; // 上货数量
                        item.subCount = 0; // 下货数量
                        item.showTable = false; // 是否详情表格
                        item.detail.forEach((item2, index) => {
                            console.log(item2)
                            if (item2.count >= 0) {
                                item.addCount += item2.count;
                            } else {
                                item.subCount += Math.abs(item2.count);
                            }
                        })
                        this.recordList.push(item);
                    })
                } else {
                    uni.showModal({
                        title: '提示',
                        content: res.returnMessage,
                        showCancel: false,
                    });
                }
            })
        },

        // 开始日期选择
        startDateChange: function(options) {
            this.startDate = options.detail.value;
            this.page = 1; // 重新筛选要恢复页码
            this.noMore = false; // 重新筛选要恢复是否没有更多的状态
            this.recordList = []; // 重新筛选要清空原有数据
            this.getAddStockRecord(); // 获取上货记录
        },

        // 结束日期选择
        endDateChange: function(options) {
            this.endDate = options.detail.value;
            this.page = 1; // 重新筛选要恢复页码
            this.noMore = false; // 重新筛选要恢复是否没有更多的状态
            this.recordList = []; // 重新筛选要清空原有数据
            this.getAddStockRecord(); // 获取上货记录
        },

        // 点击进入个人中心
        personal: function() {
            uni.navigateTo({
                url: '/pages/mine/mine'
            });
        },

        // 上货库存
        stock: function() {
            uni.redirectTo({
                url: '../stock/stock'
            });
        },

        // 上货记录
        record: function() {
            uni.redirectTo({
                url: '../record/record'
            });
        },

        // 首页点击
        home: function() {
            uni.redirectTo({
                url: '/pages/indexManage/indexManage',
            });
        },

        // 表格显示
        showTable: function(index) {
            this.recordList[index].showTable = !this.recordList[index].showTable;
        }
    },

}